/**
 * File: SplitUtil.java
 * Created by: mhaimel
 * Created on: 9 Apr 2009
 * CVS:  $Id: SplitUtil.java,v 1.2 2009/06/19 13:02:49 mhaimel Exp $
 */
package uk.ac.ebi.curtain.utils.file;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;

import uk.ac.ebi.curtain.utils.CurtainUncheckedException;
import uk.ac.ebi.curtain.utils.data.IdentifierProcessor;

/**
 * Provides functionality to split/convert one file into 1-n files
 * 
 * @author mhaimel
 *
 */
public interface SplitUtil {
	
	/**
	 * Splits one file into n files in round robin style ->
	 * e.g. Entries 1,2,3,4 get split into files A,B,C like
	 * 1->A; 2->B; 3->C; 4->A; 5->B ...
	 * 
	 * Changes the format of the identifier from the source file to the 
	 * target file (e.g. .F .R -> /1 /2)
	 * 
	 * @param from 
	 * @param to
	 * @param source Source file, which get's split
	 * @param targetArr n target Files
	 * @throws CurtainUncheckedException in case something goes wrong
	 */
	public void split(IdentifierProcessor from, IdentifierProcessor to, File source, File ... targetArr) throws CurtainUncheckedException;
	
	/**
	 * 
	 * @see #split(IdentifierProcessor, IdentifierProcessor, File, File...)
	 * @param source Source file, which get's split
	 * @param fileArr n target Files
	 * @throws CurtainUncheckedException in case something goes wrong
	 */
	public void split(File source, File ... fileArr) throws CurtainUncheckedException;

	/**
	 * Process the input of a {@link BufferedReader} and calls {@link SplitFileProcessor#processLine(String, long, long, boolean)} 
	 * for each line found.
	 * 
	 * @param from {@link IdentifierProcessor} Input format
	 * @param to {@link IdentifierProcessor} Output format
	 * @param in {@link BufferedReader} input reader to read from
	 * @param proc {@link SplitFileProcessor} processor to delegate the processing to
	 * @throws IOException
	 */
	public void split(IdentifierProcessor from, IdentifierProcessor to,BufferedReader in, SplitFileProcessor proc) throws IOException;
	
	
	public static interface SplitFileProcessor{
		/**
		 * 
		 * @param line String current line without new line at the end
		 * @param lineNumber current line which gets processed, starting from 0..n
		 * @param entryNumber current entry number, starting from 0..n
		 * @param startOfNewEntry {@link Boolean} indicates the start line of a new entry.
		 * @return boolean <code>true</code> to continue to process next line
		 */
		public boolean processLine(String line,long lineNumber, long entryNumber, boolean startOfNewEntry);
	}



	
}
